﻿using QuanLyGiaoVuDAL.Repositories.DatabaseRepositories;
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Text;

namespace QuanLyGiaoVuService.CommonServices
{
    public class FileService : IFileService
    {
        private IFileRepository _fileRepository;
        public FileService(IFileRepository fileRepository)
        {
            _fileRepository = fileRepository;
        }

        public bool DeleteFileById(Guid fileId)
        {
            return _fileRepository.DeleteFileById(fileId);
        }

        public bool GetFileStreamById(Guid fileId, out System.IO.Stream fileStream, out String fileName, out string contentType)
        {
            return _fileRepository.GetFileStreamById(fileId, out fileStream, out fileName, out contentType);
        }

        public bool SaveFileFromStream(Guid? fileId, System.IO.Stream dataStream, String fileName, String contentType, out Guid newFileId)
        {
            return _fileRepository.SaveFileFromStream(fileId, dataStream, fileName, contentType, out newFileId);
        }

        public Guid CopyFile(Guid fileId)
        {
            Guid newFileId;
            Stream fileStream;
            string fileName, contentType;
            if (_fileRepository.GetFileStreamById(fileId, out fileStream, out fileName, out contentType))
            {
                _fileRepository.SaveFileFromStream(null, fileStream, fileName, contentType, out newFileId);
                return newFileId;
            }
            else return Guid.Empty;
        }
    }
}
